<?php

namespace App\Livewire;

use App\Models\DockerModel;
use App\Models\ServerModel;
use App\Models\SessionsModel;
use Livewire\Attributes\Computed;
use Livewire\Component;
use Services\Commands\CommandService;
use Services\Constants\ConnectionsConstants;
use Services\Constants\SessionsConstants;

class DockerComponent extends Component
{
    public $ip;

    public $connection;

    public $service;

    public function mount($ip, $connection, $service)
    {
        $this->ip = $ip;
        $this->connection = $connection;
        $this->service = $service;
    }

    public function createWatcher()
    {
        $watcherSession = new SessionsModel();
        $watcherSession->user_id = auth()->user()->id;
        $watcherSession->server = $this->server->ip_address;
        $watcherSession->start_time = time();
        $watcherSession->active = 1;
        $watcherSession->connection_id = 1;
        $watcherSession->parameters = '{"user": "root", "password": "3w3uxhXmmh"}';
        $watcherSession->type = 'admin';
        $watcherSession->save();
    }

    #[Computed]
    public function containers()
    {
        $server = ServerModel::query()->where('ip_address', '=', $this->ip)->first();
        $watcherSession = $server->sessions()->where([['type', '=', 'admin'], ['active', '=', 1]])
            ->first();
        app(CommandService::class)
            ->interact($watcherSession->user_id,
                ConnectionsConstants::CONNECTIONS_ID_MAPPERS[$watcherSession->connection_id],
                SessionsConstants::DEFAULT_SESSION,
                'docker ps',
                ['port' => $server->port, 'host' => $server->ip_address, 'properties' => json_decode($watcherSession->parameters, true)],
                'services');

        $containers = DockerModel::query()->where('server', '=', $this->ip)->get();
        $containersData = [];

        foreach ($containers as $container) {
            $container->containers = str_replace('IMAGE', '', $container->containers);
            $container->containers = str_replace('NAMES', '', $container->containers);
            $container->containers = str_replace('CONTAINER ID ', '', $container->containers);
            $container->containers = str_replace('COMMAND', '', $container->containers);
            $container->containers = str_replace('CREATED', '', $container->containers);
            $container->containers = str_replace('STATUS', '', $container->containers);
            $container->containers = str_replace('PORTS', '', $container->containers);

            $container = explode('   ', $container->containers);
            $container = array_filter($container, function ($elem) {
                return $elem !== '';
            });
            $container = array_map(function ($elem) {
                return trim($elem);
            }, $container);

            $containersData[] = array_values($container);
        }
        $containersData = array_filter($containersData, fn ($value) => $value !== '');

        return $containers == null ? null : $containersData;
    }

    public function render()
    {
        return view('livewire.server.services.docker.docker-component');
    }

    public function delete($hash)
    {
        DockerModel::query()->where('hash', '=', $hash)->delete();
    }

    public function reload($hash)
    {
        sleep(5);
        DockerModel::query()->where('hash', '=', $hash)->update(
            ['containers' => '         CONTAINER ID   IMAGE                      COMMAND                  CREATED       STATUS       PORTS                                                                                                                                                 NAMES
7d64deb2084c   rabbitmq:3.13-management   "docker-entrypoint.s…"   15 seconds ago   Up 15 seconds   4369/tcp, 5671/tcp, 0.0.0.0:5672->5672/tcp, :::5672->5672/tcp, 15671/tcp, 15691-15692/tcp, 25672/tcp, 0.0.0.0:15672->15672/tcp, :::15672->15672/tcp   rabbitmq']
        );
        file_put_contents('hash.txt', $hash);
    }
}
